package com.appkubes.mall.admin.application.system.announcement.controller;


import com.appkubes.common.Response;
import com.appkubes.mall.admin.application.base.BaseController;
import com.appkubes.mall.admin.application.system.announcement.service.AnnouncementService;
import com.appkubes.mall.admin.utils.PageUtil;
import com.appkubes.mall.core.po.MallAnnouncement;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api("公告模块")
@RestController
@RequestMapping("/sass_admin/Sannouncement")
public class AnnouncementController extends BaseController {


    @Autowired
    AnnouncementService announcementService;

    @ApiOperation(value = "公告列表")
    @PostMapping("/getList")
    public Response getList(@RequestParam(defaultValue = "") String key,
                            @RequestParam(defaultValue = "1") Integer page,
                            @RequestParam(defaultValue = "10") Integer limit,
                            @RequestParam(defaultValue = "id") String orderfield,
                            @RequestParam(defaultValue = "desc") String ordertype) {
        PageInfo<MallAnnouncement> pageinfo = announcementService.querySelective(key,getAppKey() ,page, limit, orderfield,ordertype);
        return Response.success(new PageUtil<MallAnnouncement>(pageinfo));
    }

    @ApiOperation(value = "保存公告")
    @PostMapping("/save")
    public Object save(MallAnnouncement model) {
        int result = 0;
        if(StringUtils.isEmpty(model.getAnnouncementKey())) model.setAnnouncementKey(null);
        if (!StringUtils.isEmpty(model.getAnnouncementKey())) {
            result = announcementService.update(model);
        }else{
            result = announcementService.add(model);
        }
        if (result>0) {
            return Response.success(result);
        } else {
            return Response.error("保存失败");
        }
    }

    @ApiOperation(value = "修改公告")
    @PostMapping("/edit")
    public Object edit(@RequestParam String announcementKey) {
        MallAnnouncement model = announcementService.findById(announcementKey);
        return Response.success(model);
    }

    @ApiOperation(value = "删除公告")
    @PostMapping("/delete")
    public Response delete(@RequestParam  String ids) {

        int result = announcementService.deleteByIds(ids.split(","));
        if(result >0){
            return Response.success(result);
        }
        return Response.error("删除失败");

    }

    @ApiOperation(value = "启用公告")
    @PostMapping("/batchenable")
    public Response batchenable(String ids) {
        int ret = announcementService.batchenable(ids.split(","));
        if (ret>0) {
            return Response.success("取消成功");
        } else {
            return Response.error("取消失败");
        }
    }

    @ApiOperation(value = "禁用公告")
    @PostMapping("/batchunenable")
    public Response batchunenable(String ids) {
        int ret = announcementService.batchunenable(ids.split(","));
        if (ret>0) {
            return Response.success("禁用成功");
        } else {
            return Response.error("禁用失败");
        }
    }
}
